/* EventColl.java
 * 
 * EventColl is a collection of events and is
 * based off a ArrayList in order to maintain order
 * in the list, with a special add method to maintain
 * order based on GregorianCalendar dates
 * 
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */

import java.util.*;

public class EventColl extends ArrayList< Event >
{
   public EventColl()
   {
      super();
   }
   
   //--------------------------accessors------------------------------
   
   //returns the start location of this list/tour
   public String startPlace()
   {
      if( this.size() > 0 )
         return this.get( 0 ).startPlace();
      else
         return "";
   }
   
   //returns the end location of this list/tour
   public String endPlace()
   {
      if( this.size() > 0 )
         return this.get( this.size() - 1 ).endPlace();
      else
         return "";
   }
   
   //returns the start time of this list/tour
   public GregorianCalendar startTime()
   {
      if( this.size() > 0 )
         return this.get( 0 ).startTime();
      else
         return null;
   }
   
   //returns the end time of this list/tour
   public GregorianCalendar endTime()
   {
      if( this.size() > 0 )
         return this.get( this.size() - 1 ).endTime();
      else
         return null;
   }
   
   
   //adds an event to the event map
   //add a procedure payment to the person
   public boolean add( Event e )
   {
      int i, result;
      
      //check if provider is not open at that time
      if( e.providerConflict() )
      {
         System.err.printf( "Event is outside operation hours!\n" );
         return false;
      }
      
      for( i = 0; i < this.size(); i++ )
      {
         result = e.compareTo( this.get( i ) );
         if( result < 0 )
            break;
         else if( result == 0 )
            return false;
      }
      this.add( i, e );
      return true;
   }
   
   //checks if new event conflicts
   public boolean conflict( Event e )
   {
      for( int i = 0; i < this.size(); i++ )
      {
         if( this.get( i ).compareTo( e ) == 0 )
            return true;
      }
      return false;
   }
   
   
   public String toString()
   {
      //creates a string representation of the events in this collection
      String s = "EVENTS:\n";
      
      for( int i = 0; i < this.size(); i++ )
         s = s + this.get( i ).toString();
      
      return s;
   }
   
}